package com.coldchain.admin.user.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.coldchain.admin.user.entity.CompanyAccount;
import com.coldchain.admin.user.mapper.AdminMapper;
import com.coldchain.admin.user.mapper.CompanyAccountMapper;
import com.coldchain.common.auth.AuthUser;

import com.coldchain.common.exception.auth.UserInvalidException;
import com.coldchain.common.feign.CarDriverFeign;
import com.coldchain.common.feign.CysFeign;
import com.coldchain.common.msg.ObjectRestResponse;



import com.coldchain.common.util.MD5Utils;
import com.fangliai.core.entitiy.SystemModelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

/**
 * @ClassName TokenService
 * @Description TODO
 * @Author fangliai
 * @Date 2018/9/11 15:51
 * @Vesion 1
**/
@Service
public class TokenService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private CarDriverFeign carDriverFeign;

    @Autowired
    private CompanyAccountMapper accountMapper;
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private CysFeign cysFeign;
    /**
     * 密码验证
     * @param scope
     * @param loginName
     * @param password
     * @return
     */
    public AuthUser passwordLogin(SystemModelEnum scope, String loginName, String password) {
        AuthUser authUser=null;
        switch (scope){
            case ADMIN:
                authUser=adminMapper.validate(loginName, MD5Utils.getMD5Str(password));
                if(authUser==null){
                    throw  new UserInvalidException("帐号/密码错误");
                  }
                authUser.setSysModel(SystemModelEnum.ADMIN);
                break;
            case USER:
                return validateUser(loginName,MD5Utils.getMD5Str(password));
            case CYS:
                ObjectRestResponse<AuthUser> cysObj=cysFeign.validateCysLogin(loginName,password);
                if(cysObj.getStatus()== HttpStatus.OK.value()){
                    authUser=cysObj.getData();
                    authUser.setSysModel(SystemModelEnum.CYS);
                }else {
                    throw  new UserInvalidException(cysObj.getMessage());
                }
                break;
            case DRIVER:
                ObjectRestResponse<AuthUser> objectRestResponse= carDriverFeign.validateCarDriver(loginName,password);
                if(objectRestResponse.getStatus()== HttpStatus.OK.value()){
                    authUser=objectRestResponse.getData();
                    authUser.setSysModel(SystemModelEnum.DRIVER);
                }else {
                    throw  new UserInvalidException(objectRestResponse.getMessage());
                }
                break;
            default:
                break;
        }
        return authUser;
    }

    /**
     * 短信验证
     * @param scope
     * @param phone
     * @param code
     * @return
     */
    public AuthUser ssmLogin(SystemModelEnum scope,String phone,String code){
        return null;
    }

    private AuthUser validateUser(String username,String password){

        AuthUser authUser=new AuthUser();
        authUser.setSysModel(SystemModelEnum.USER);
        CompanyAccount account=accountMapper.selectOne(new QueryWrapper().eq(true,"accountPhone",username).or(true).eq(true,"email",username));
        if(account==null){
            throw  new UserInvalidException("用户不存在");
        }
        if(!password.equals(account.getPassword())){
            throw  new UserInvalidException("密码错误");
        }
        authUser.setNickName(account.getAccountName());
        authUser.setCommpanyId(account.getCompanyId());
        authUser.setUserId(account.getAccountId());
        authUser.setUserName(account.getAccountPhone());
        return authUser;
    }
}
